#include "gfx_sprite.h"

#ifndef UGLY_HACK_FOR_THOSE_WHO_DONT_KNOW_HOWTO_INSTALL_SDL
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

GFX_Sprite* gfx_sprite_new(const char *image)
{
    GFX_Sprite *sprite= mem_allocate(sizeof(GFX_Image));
    GFX_Image *nom=gfx_image_get (image);
    sprite->image=nom;
    sprite->frame=0;
    sprite->x=0;
    sprite->y=0;
    return sprite;
}

void gfx_sprite_delete(GFX_Sprite *sprite)
{
    mem_free (sprite);
}

void gfx_render_sprite(GFX_Sprite *sprite)
{
    SDL_Surface *_Ecran = SDL_GetVideoSurface();

    if(sprite == 0) { return; }
    if(sprite->image == 0)
    {
        log_puts(LOG_WARN, "Tried to render a GFX_Sprite without an image");
        return;
    }
    SDL_Rect src, dest;

    src.w = sprite->image->width  / sprite->image->columns;
    src.h = sprite->image->height / sprite->image->rows;

    src.x = ((sprite->frame % sprite->image->columns) * src.w);
    src.y = ((sprite->frame / sprite->image->columns) * src.h);

    dest.x = sprite->x - sprite->image->xoffset + _Ecran->clip_rect.x;
    dest.y = sprite->y - sprite->image->yoffset + _Ecran->clip_rect.y;

    SDL_BlitSurface(sprite->image->image, &src, _Ecran, &dest);
    return;
}
